(function() {
	var ListMenu = G.GClass('Widget', 'ListMenu', G.Base.EventListener);

	ListMenu.prototype.__init = function(items)
	{
		this.__Base__EventListener__init();
		this._items = items;
		this._itemByName = {};
		this._current = null;
	}

	ListMenu.prototype.render = function(container)
	{
		var box = document.createElement('div');
		for (var i = 0; i < this._items.length; i++) {
			var item = this._items[i];
			var li = document.createElement('li');
			li.name = item.name;
			li.innerHTML = item.title;

			this._itemByName[item.name] = {'index': i, 'element': li};
			box.appendChild(li);
		}
		box.className = 'listmenu';
		container.appendChild(box);
		G(box).bind('onclick', this._on_click, this);
	}

	ListMenu.prototype._on_click = function(evt)
	{
		var em = evt.srcElement;

		if (this._current != null) {
			this._current.className = '';
		}
		if (em.name) {
			this.fire('onitemclick', em.name);
		}
		this._current = em;
		em.className = 'current';
	}

	ListMenu.prototype.select_item = function(indx)
	{
		if (indx >= this._items.length)	return;
		
		var name = this._items[indx].name;
		var li = this._itemByName[name].element;
		if (this._current != null) {
			this._current.className = '';
		}
		this._current = li;
		li.className = 'current';
	}

	G.DOM.Element.prototype.ListMenu = function(items) 
	{
		var lm = new G.Widget.ListMenu(items);
		lm.render(this.em);
		return lm;
	}

 })();
